﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Linq.Expressions;
using BitPortal.Utils;


namespace BitPortal.Business
{
	public static partial class ProductCustomerBLL
	{
        /* **************************** INSERT - UPDATE - DELETE **************************************** */

		public static int Insert(this ProductCustomer ProductCustomer)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			extBitPortal.ProductCustomers.AddObject(ProductCustomer);
			return extBitPortal.SaveChanges();
		}

        public static int DeletePermanently(Int64 productCustID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			ProductCustomer item = extBitPortal.ProductCustomers.SingleOrDefault(pp => pp.ProductID == productCustID);
			extBitPortal.ProductCustomers.DeleteObject(item);
			return extBitPortal.SaveChanges();
		}

        
		public static int Update(this ProductCustomer ProductCustomer)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			ProductCustomer item = extBitPortal.ProductCustomers.SingleOrDefault(pp => pp.ProductID == ProductCustomer.ProductID);
			item = ProductCustomer;
			return extBitPortal.SaveChanges();
		}


        /* ********************************************** GET PRODUCTS OF CUSTOMER ********************* */

        /// <summary>
        /// Get all products of an customers by customerid
        /// </summary>
        /// <param name="customerID"></param>
        /// <returns></returns>
		public static IQueryable<ProductCustomer> GetProductByCustomerID(Int64 customerID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();
			return extBitPortal.ProductCustomers.Where(pp => pp.CustomerID == customerID);
		}


		/// <summary>
		/// Get all products of an customer
		/// </summary>
		/// <returns></returns>
        public static IQueryable<ProductCustomer> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
          
            IQueryable<ProductCustomer> list = from pp in bitEnt.ProductCustomers                                              
                                               select pp;         
            return list;
        }

        
        /// <summary>
        /// Get all products of an custormer by customer's userID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static IQueryable<ProductCustomer> GetAllByUser(Guid userID)
        {
            ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

            IQueryable<ProductCustomer> products = from user in extBitPortal.aspnet_Users
                                                   from cust in extBitPortal.Customers
                                                   from prodCustomer in extBitPortal.ProductCustomers
                                                   where user.UserId == userID && user.UserId == cust.UserID &&
                                                          cust.CustomerID == prodCustomer.CustomerID
                                                   orderby prodCustomer.ProductCustomerId descending
                                                   select prodCustomer;
            return products;
        }

        /// <summary>
        /// Increment number of errors
        /// </summary>
        /// <param name="productCustomerID"></param>
        public static void AddCountError(long productCustomerID)
        {
            ExtendedBitPortalEntities ent = new ExtendedBitPortalEntities();
            ProductCustomer item = ent.ProductCustomers.SingleOrDefault(pp => pp.ProductCustomerId == productCustomerID);
            item.CountError++;
            ent.SaveChanges();
        }
    }
}